package cn.ctvonline.android.kaopu.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import cn.ctvonline.android.kaopu.R;

/**
 * Created by dujian on 15/12/8.
 */
public class FloatingWindow extends PopupWindow {
    static final String TAG = "FloatingWindow";
    private int radius = 150;
    private View contentView;
    private int showX;
    private int showY;
    private Context context;
    int orgX, orgY;
    int offsetX, offsetY;
    int statusBarHeight;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
//            msgTv.setText(msg.obj.toString());
        }
    };

    //    private TextView msgTv;
    public void init(Context context) {
        this.context = context;
        contentView = LayoutInflater.from(context).inflate(R.layout.floating_view, null, false);
//        msgTv = (TextView) contentView.findViewById(R.id.floating_msg_tv);
        Rect frame = new Rect();
        contentView.getWindowVisibleDisplayFrame(frame);
        statusBarHeight = frame.top;
        setContentView(contentView);
        setOutsideTouchable(false);
        setWidth(radius);
        setHeight(radius);
        setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(android.R.color.transparent)));
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getPointerCount() != 1) {
                    return false;
                }
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        orgX = (int) event.getX();
                        orgY = (int) event.getY();
                        return false;
                    case MotionEvent.ACTION_MOVE:
                        offsetX = (int) event.getRawX() - orgX;
                        offsetY = (int) event.getRawY() - orgY;
                        update(offsetX, offsetY, -1, -1, true);
                        return true;
                    case MotionEvent.ACTION_UP:
                        return false;
                }
                return true;
            }
        });
    }

    public void show(View parentView) {
        showX = parentView.getMeasuredWidth() - radius - 20;
        showY = (int) parentView.getY() + parentView.getMeasuredHeight() - radius - 20;
        Log.i(TAG, "show : showX=" + showX + ",showY=" + showY);
        showAtLocation(parentView, Gravity.RIGHT | Gravity.BOTTOM, showX, showY);
    }

    public void update(int x, int y) {
        Log.i(TAG, "update x=" + x + ",y=" + y);
        update(x, y, radius, radius, true);
    }
}
